package com.baizhi.service.impl;

import com.baizhi.dao.ClassDAO;
import com.baizhi.dao.TagDAO;
import com.baizhi.entity.Class;
import com.baizhi.entity.Tag;
import com.baizhi.service.ClassService;
import com.baizhi.service.tagService;
import com.baizhi.util.MybatisUtil;

import java.util.List;

public class ClassServiceImpl implements ClassService {


    @Override  //查询所有
    public List<Class> queryAll() {
            List<Class> list = null;
            try {

                ClassDAO cd = (ClassDAO) MybatisUtil.getMapper(ClassDAO.class);
                list = cd.selectAll();
                MybatisUtil.commit();

            }catch (Exception e){
                e.printStackTrace();
                MybatisUtil.rollback();  //向上回滚
                throw new RuntimeException(e);
            }finally {
                MybatisUtil.close();  //关闭资源
            }
            return list;
    }



    @Override
    public void deleteById(int id) {
        try {

            ClassDAO cd = (ClassDAO) MybatisUtil.getMapper(ClassDAO.class);
            cd.deleteById(id);
            MybatisUtil.commit();
        }catch (Exception e){
            e.printStackTrace();
            MybatisUtil.rollback();  //向上回滚
            throw new RuntimeException(e);

        }finally {
            MybatisUtil.close();  //关闭资源
        }
    }


/* 插入一个班级 */
    @Override
    public void insertClass(Class clazz) {
        try{

            ClassDAO cd = (ClassDAO) MybatisUtil.getMapper(ClassDAO.class);
            cd.insertClass(clazz);
            MybatisUtil.commit();
        }catch (Exception e){
            e.printStackTrace();
            MybatisUtil.rollback();  //向上回滚
            throw new RuntimeException(e);
        }finally {
            MybatisUtil.close();  //关闭资源
        }
    }

    /* 查询班级对应的标签 */
    @Override
    public List<Tag> queryClazzTag() {

        List<Tag> list = null;
        try {
            ClassDAO cd = (ClassDAO) MybatisUtil.getMapper(ClassDAO.class);
            list = cd.queryClazzTag();
            MybatisUtil.commit();

        }catch (Exception e){
            e.printStackTrace();
            MybatisUtil.rollback();  //向上回滚
            throw new RuntimeException(e);
        }finally {
            MybatisUtil.close();  //关闭资源
        }
        return list;
    }
}
